package com.wlr.study.leetcode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * 字母异位词分组
 */
public class LeetCode49 {

    public static List<List<String>> groupAnagrams(String[] strs) {
        HashMap<String, List<String>> map = new HashMap<>();
        for (String str: strs) {
            char[] charArray = str.toCharArray();
            Arrays.sort(charArray);
            List<String> containsList;
            if(map.containsKey(Arrays.toString(charArray))){
                containsList = map.get(Arrays.toString(charArray));
            }else{
                containsList=new ArrayList<>();
            }
            containsList.add(str);
            map.put(Arrays.toString(charArray), containsList);
        }
        return new ArrayList<>(map.values());
    }

    public static void main(String[] args) {
        String[] strs = new String[]{"eat", "tea", "tan", "ate", "nat", "bat"};
        System.out.println(LeetCode49.groupAnagrams(strs));
    }
}
